<?php
/**
 * 搜索控制器
 */
class SearchController extends CommonController{
	
	//列表首页
	public function index(){
		
		
		//查询用户搜索关键词
		$search = Q('get.s');
		$this->assign('search',$search);
		//判断排序
		$sort = empty($_GET['sort'])?0:$_GET['sort'];
		switch($sort){
			case 1:
				$sort = 'goods_addTime desc';
				break;
			case 2:
				$sort = 'goods_click desc';
				break;
			case 3:
				$sort = 'goods_mall asc';
				break;
			default:
				$sort = '';
				break;
		}
		if($sort == ''){
			$searchData = count(M()->JOIN("__goods__ g  JOIN __goods_info__ info ON g.gid = info.goods_gid")->where("goods_name like '%$search%' ")->all());
		}else{
			$searchData = count(M()->JOIN("__goods__ g  JOIN __goods_info__ info ON g.gid = info.goods_gid")->where("goods_name like '%$search%' ")->order($sort)->all());
		}

		
		//获取栏目id
		$cid = Q('get.cid',0,'intval');
		
		//查询热门商品
		$goodsHot = M()->JOIN("__goods__ g  JOIN __goods_info__ info ON g.gid = info.goods_gid")->where("goods_hot = '1' ")->limit(10)->all();
		
		//重组热们商品图片
		foreach($goodsHot as $k => $v){
			$imgUrl = array_filter(explode(',', $v['goods_info_smallIMg']));//将字符串转数组 并过滤掉空值
			if($imgUrl){
				$imgUrl = array_splice($imgUrl,0,1);
				$goodsHot[$k]['imgUrl'] = $imgUrl[0];//抽取一张做列表图
			}else{
				$goodsHot[$k]['imgUrl'] = 0;
			}

		}
		
		//卖家推荐
		$goodsdesc = M()->JOIN("__goods__ g  JOIN __goods_info__ info ON g.gid = info.goods_gid")->order('goods_click desc')->limit(4)->all();
		
		//重组热们商品图片
		foreach($goodsdesc as $k => $v){
			$imgUrl = array_filter(explode(',', $v['goods_info_smallIMg']));//将字符串转数组 并过滤掉空值
			if($imgUrl){
				$imgUrl = array_splice($imgUrl,0,1);
				$goodsdesc[$k]['imgUrl'] = $imgUrl[0];//抽取一张做列表图
			}else{
				$goodsdesc[$k]['imgUrl'] = '0';
			}

		}
		
		//栏目分类
		$category = M('category')->where("cid = '{$cid}'")->find();
		$pid = $category['pid'];
		
		//查找父级栏目
		$category = M('category')->where("pid = '{$pid}'")->all();
		
		//循环组合父级栏目的子栏目
		foreach($category as $k =>$v ){
			//用$v的cid 查询栏目下所有pid和cid 相同的子栏目
			$cid = $v['cid'];
			$Soncategory = M('category')->where("pid = '{$cid}'")->all();
			//判断是否有子栏目 如果有就查询子栏目  没有就查询父级栏目 再组合子栏目
			if(empty($Soncategory)){
				$pid = $v['pid'];									
				$Soncategory = M('category')->where("pid = '{$pid}'")->all();
			}
			//判断返回是否存在栏目
			if($Soncategory){
				foreach($Soncategory as $kk => $vv){
					$category[$k]['son']['category_name'][$kk] = $vv['category_name'];
					$category[$k]['son']['cid'][$kk] = $vv['cid'];
				}
			}else{//有子栏目就组合数据  没有就设置空
				$category[$k] =null;
			}			
		}
		$category = array_filter($category);
		$this->assign('category',$category);
		$this->assign('goodsdesc',$goodsdesc);
		$this->assign('goodsHot',$goodsHot);
		


		

	

		//分页
		$total = $searchData;
		$this->assign('total',$total);
		$pageNum =  (isset($_GET['page']))?(int)$_GET['page']:0;
		$this->assign('pageNUm',$pageNum);
		//实例化分页  ('总条数'，'每页显示条数','显示页码数')
		$pageObj = new Page($total,16,2);
		$page = $pageObj->show();
		$this->assign('page',$page);
		$limit = $pageObj->limit();
	

		//当前页显示的最大条数
		$num = $limit*$pageNum+$limit;
		//当前页页面已显示条数
		$nowNum = count($limit);
		$this->assign('nowNum',$nowNum);
		$lim = explode(',' ,$limit);
		$lim = $lim['1'];
		$this->assign('lim',$lim);
		if($sort == ''){
			$searchData = M()->JOIN("__goods__ g  JOIN __goods_info__ info ON g.gid = info.goods_gid")->where("goods_name like '%$search%' ")->limit($limit)->all();;
		}else{
			$searchData = M()->JOIN("__goods__ g  JOIN __goods_info__ info ON g.gid = info.goods_gid")->where("goods_name like '%$search%' ")->order($sort)->limit($limit)->all();
		}

		
		//如果查询有返回 就查询商品信息 分配视图
		if(count($searchData)>=1){
			//重组图片调用路径
			foreach($searchData as $k => $v){
				$imgUrl = array_filter(explode(',', $v['goods_info_smallIMg']));//将字符串转数组 并过滤掉空值
				if($imgUrl){
					$imgUrl = array_splice($imgUrl,0,1);
					$searchData[$k]['imgUrl'] = $imgUrl[0];//抽取一张做列表图
				}else{
					$searchData[$k]['imgUrl'] = 0;
				}

			}
			$this->assign('searchData',$searchData);
		}

		//调用模板
		$this->dis('search');
	}

	
	//递归查询cid 
	public function _getSonCid($data,$cid){
		//临时变量
		$temp = array();
		//循环所有栏目 进行判断
		foreach($data as $v){
			//如果pid和cid 相等即栏目下的子栏目 压入cid
			if($v['pid']==$cid){
				$temp[] = $v['cid'];
				//合并所有cid
				$temp = array_merge($temp,$this->_getSonCid($data, $v['cid']));
			}
		}
		return $temp;
	}
}
